using System.Text;

namespace Bosphorus.Integration.Jira.Thor.Dal.Dao.Model
{
    partial class IssueDao
    {
        internal class SqlBuilder
        {
            private readonly StringBuilder stringBuilder;
            private const string STATUS_RESOLVED = "5";
            private const string STATUS_CLOSED = "6";
            private const string CRITERIA_FORMAT = " and ({0}) ";
            private const string STATUS_FORMAT = "i.Status not in('{0}','{1}')";

            private SqlBuilder()
            {
                const string sqlFormat = "select i from Issue as i join i.Project as p where (1=1)";
                stringBuilder = new StringBuilder(sqlFormat);
            }

            public static SqlBuilder New()
            {
                return new SqlBuilder();
            }

            public SqlBuilder WithProjectKey(string projectKey)
            {
                stringBuilder.AppendFormat(CRITERIA_FORMAT, "p.Key = ?");
                return this;
            }

            public SqlBuilder WhereStatusOpen()
            {
                string statusCriteria = string.Format(STATUS_FORMAT, STATUS_RESOLVED, STATUS_CLOSED);
                stringBuilder.AppendFormat(CRITERIA_FORMAT, statusCriteria);
                return this;
            }

            public  string  Build()
            {
                return stringBuilder.ToString();
            }
        }
    }
}